package edu.ynu.se.xiecheng.achitectureclass.common.utils;

import edu.ynu.se.xiecheng.achitectureclass.common.service.CameraRecodeService;
import edu.ynu.se.xiecheng.achitectureclass.common.service.CommonRecodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class TokenManager {

    private static String commonToken;
    private static String cameraToken;

    @Autowired
    private CommonRecodeService commonRecodeService;

    @Autowired
    private CameraRecodeService cameraRecodeService;

    public synchronized String getCommonToken() {
        if (commonToken == null) {
            try {
                commonToken = commonRecodeService.getCommonToken();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return commonToken;
    }

    public synchronized String getCameraToken() {
        if (cameraToken == null) {
            try {
                cameraToken = cameraRecodeService.getCameraToken();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return cameraToken;
    }

    public synchronized void refreshCommonToken() {
        try {
            commonToken = commonRecodeService.getCommonToken();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public synchronized void refreshCameraToken() {
        try {
            cameraToken = cameraRecodeService.getCameraToken();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
